var rootPath = '';//kk.getRootPath();

/* bookCodeTableFunc() */
function bookCodeTableFunc(bid, bookIndex, bookName) {
	/* bookCodeTableDom */
	var bookCodeTableDom = '\
	<div class="box box-skin-inner sub-content">\
		<div class="box-header with-border">\
			<span class="box-title">' + bookName + '</span>\
			<div class="box-tools pull-right">\
				<button type="button" class="btn btn-box-tool btnCancel" data-widget="remove"><i class="fa fa-times"></i></button>\
			</div>\
		</div>\
		<div class="box-body no-padding">\
			<div class="row">\
				<div class="col-sm-12">\
				    <div id="bookCodeToolbar">\
						<div class="form-inline" role="form">\
					    	<div class="form-group btn-group">\
			        			<button class="btn btn-default btn-flat" onclick="CRUDBookCodeFunc(1, null, null, ' + bid + ')"><i class="fa fa-plus"></i></button>\
								<button class="btn btn-default btn-flat" onclick="bookCodeReloadFunc()"><i class="fa fa-refresh"></i></button>\
							</div>\
			            </div>\
				    </div>\
					<table id="bookCodeTable"></table>\
				</div>\
			</div>\
		</div>\
		<div class="box-footer text-center">\
			<button type="button" class="btn btn-info btn-flat btnCloseAndSave">关闭并更新</button>\
			<button type="button" class="btn btn-default btn-flat btnCancel">关闭</button>\
		</div>\
	</div>\
	';
	
	$('.main-content').hide();
	$('.main-content').after(bookCodeTableDom);
	
	$('#bookCodeTable').bootstrapTable({
		classes: 'table table-no-bordered table-hover',
		striped: true,
		toolbar: '#bookCodeToolbar',
		sidePagination: 'server',
		url: rootPath + '/Book/BookCode/dataListByBid?bid=' + bid,
	    idField: 'id',
	    columns: [{
            field: 'state',
            checkbox: true,
            visible: false,
//	        width: '20%',
            align: 'left',
            valign: 'middle',
        }, {
	        title: 'ID',
	        field: 'id',
	        sortable: true,
//	        width: '20%',
            align: 'left',
            valign: 'middle',
	    }, {
	        title: '采购人',
	        field: 'purchaser',
//	        width: '20%',
            align: 'left',
            valign: 'middle',
            formatter: function(value, row, index) {
    			return value != '' ? value : '<span class="text-gray">未填写</span>';
    		},
	    }, {
	        title: '采购价格',
	        field: 'purchase_price',
//	        width: '20%',
            align: 'left',
            valign: 'middle',
            formatter: function(value, row, index) {
    			return value > 0 ? value : '<span class="text-gray">未填写</span>';
    		},
	    }, {
	        title: '采购渠道',
	        field: 'purchase_channel',
//	        width: '20%',
            align: 'left',
            valign: 'middle',
            formatter: function(value, row, index) {
    			return value != '' ? value : '<span class="text-gray">未填写</span>';
    		},
	    }, {
	        title: '添加时间',
	        field: 'add_time',
	        sortable: true,
//	        width: '20%',
            align: 'left',
            valign: 'middle',
	    }, {
            title: '操作',
            field: 'operate',
//	        width: '20%',
            align: 'left',
            valign: 'middle',
            formatter: function(value, row, index) {
				var str = '';
				str += '<div class="btn-group">';
				str += '<button class="btn btn-default btn-xs" onclick="CRUDBookCodeFunc(3, ' + row.id + ', ' + index + ');">编辑</button>';
				str += '<button class="btn btn-default btn-xs" onclick="CRUDBookCodeFunc(4, ' + row.id + ');">删除</button>';
				str += '</div>';
				return str;
			},
        }],
	});
	
	$('.btnCloseAndSave').off('click').on('click', function() {
		// 更新spuIndex +++
    	// 关闭时，更新book表以更新book对应的bookCode信息
        //	console.log(bookIndex);
			url = rootPath + '/Book/Book/refresh?id='+ bid;
			method = "PUT";
	
			result = kk.doStrAjax(url, method, null);
		    if (result.code == 0) {
		    	$('#table').bootstrapTable('updateRow', {
		    		index: bookIndex,
					row: result.obj
		    	});
	        	
	    		$('.sub-content').remove();
	    		$('.main-content').show();
			}
	});
	
	$('.btnCancel').off('click').on('click', function() {
		$('.sub-content').remove();
		$('.main-content').show();
	});
	
}

/* reload */
function bookCodeReloadFunc() {
	$('#bookCodeTable').bootstrapTable('refresh');
}